﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title = "NewProducts";
}

<h2>NewProducts</h2>
 <!-- ko foreach: ProductList -->
    @Html.Partial("ProductList.SingleProduct")
 <!-- /ko -->


@{
    var model = ViewBag.Model;
    var jsonData = new HtmlString(new JavaScriptSerializer().Serialize(model));
}



    @Scripts.Render("~/scripts/Product/Product.js")
@section scripts{
    <script>

        var init = @jsonData;

      
        function NewProductModel() {

            var self = this;
           
            var tempProduct = $.isEmptyObject(init) ? [] : $.map(init, function(a) {
                var picModel = new PictureModel(a.DefaultPictureModel.ImageUrl, a.DefaultPictureModel.FullSizeImageUrl, a.DefaultPictureModel.Title);
                var tempSpecAttributes= $.isEmptyObject(a.SpecificationAttributeModels) ? [] : $.map(a.SpecificationAttributeModels, function(c) {
                    return new ProductSpecificationAttributeModel(c.AttributeName, c.AttributeOptionName);
                });
                return new ProductOverviewModel(a.Id, a.Name, a.ShortDescription, a.FullDescription, a.MinPrice, picModel, tempSpecAttributes);
            });

            self.ProductList = ko.observableArray(tempProduct);

        }

        var vm = new NewProductModel();
        ko.applyBindings(vm);
        
    </script>
    

}